'''我的新kfol(咕咕镇)脚本'''

## 设置.in文件位置
IN_FILE = r'E:\PC\newkf\newkf.in'

mo1 = {'光环': 77, '角色': '默', '等级': 209, '技能位': 3,
  #'加点': [1, 208, 325, 39, 30, 30], # lv max = 209, 加点总数 = 633
  '加点': [1, 158, 275, 1, 197, 1],
  '武器': ['剑', 52, 101, 98, 73, 87],
  # '武器': ['弓', 36, 87, 118, 84, 134],
  # '武器': ['刃', 44, 98, 63, 94, 69],
  # '手套': ['套', 47, 100, 109, 107, 108],
  '手套': ['环', 50, 76, 99, 97, 82],
  #'护甲': ['铁', 40, 85, 131, 111, 114],
  # '护甲': ['皮', 36, 123,118,125,105],
  #'护甲': ['布', 36, 73, 129, 135, 125],
  '护甲': ['袍', 50, 132, 57, 122, 81],
  '头饰': ['巾', 41, 132, 111, 115, 116],
  '技能': [
    '启程之誓',
    '启程之心',
    '启程之风',
    #'破壁之心',
    #'破魔之心',
    #'伤口恶化',
    #'精神创伤',
    #'铁甲尖刺',
    #'沸血之志',
    #'波澜不惊',
    #'飓风之力',
    '' # 结束符,不用管它
  ]
}

meng1 = {'光环': 80, '角色': '梦', '等级': 214, '技能位': 4,
  '加点': [1, 265, 165, 1, 215, 1], # lv max = 214, 加点总数 = 648
  '武器': ['剑', 52, 101, 98, 73, 87],
  # '武器': ['弓', 36, 87, 118, 84, 134],
  # '武器': ['刃', 44, 98, 63, 94, 69],
  # '手套': ['套', 47, 100, 109, 107, 108],
  '手套': ['环', 50, 76, 99, 97, 82],
  #'护甲': ['铁', 40, 85, 131, 111, 114],
  # '护甲': ['皮', 36, 123,118,125,105],
  #'护甲': ['布', 36, 73, 129, 135, 125],
  '护甲': ['袍', 50, 132, 57, 122, 81],
  '头饰': ['巾', 41, 132, 111, 115, 116],
  '技能': [
    '启程之誓',
    '启程之心',
    '启程之风',
    #'破壁之心',
    '破魔之心',
    #'伤口恶化',
    #'精神创伤',
    #'铁甲尖刺',
    #'沸血之志',
    #'波澜不惊',
    #'飓风之力',
    '' # 结束符,不用管它
  ]
}

lin1 = {'光环': 70, '角色': '琳', '等级': 233, '技能位': 3,
  '加点': [234+10, 434+17, 1, 1, 1, 1], # lv max = 222, 加点总数 = 672
  #'加点': [234, 434, 1, 1, 1, 1], - 44级刃,50层极大概率击破MU,ZHU,DENG的3级
  #'武器': ['剑', 52, 101, 98, 73, 87],
  #'武器': ['弓', 36, 87, 118, 84, 134],
  '武器': ['刃', 59, 86, 93, 125, 69],
  '手套': ['套', 47, 100, 109, 107, 108],
  #'手套': ['环', 50, 76, 99, 97, 82],
  #'护甲': ['铁', 40, 85, 131, 111, 114],
  # '护甲': ['皮', 36, 123,118,125,105],
  #'护甲': ['布', 36, 73, 129, 135, 125],
  '护甲': ['袍', 50, 132, 57, 122, 81],
  '头饰': ['巾', 49, 136, 124, 111, 84],
  '技能': [
    #'启程之誓',
    '启程之心',
    '启程之风',
    #'破壁之心',
    #'破魔之心',
    #'伤口恶化',
    '精神创伤',
    #'铁甲尖刺',
    #'沸血之志',
    #'波澜不惊',
    #'飓风之力',
    '' # 结束符,不用管它
  ]
}

mo_test1 = {'光环': 70, '角色': '默', '等级': 209, '技能位': 3,
  '加点': [210, 210, 210, 1, 1, 1], # lv max = 209, 加点总数 = 633
  #'武器': ['剑', 36, 100, 100, 150, 100],
  #'武器': ['杖', 36, 100, 100, 100, 100],
  '武器': ['杖', 36, 100, 100, 100, 100],
  # '武器': ['弓', 36, 87, 118, 84, 134],
  # '武器': ['杖', 36, 102,51,99,122],
  # '武器': ['刃', 36, 117,76,136,70],
  #'手套': ['套', 32, 100,126,101,102],
  '手套': ['环', 32, 113, 150, 99, 127],
  '护甲': ['铁', 40, 85, 131, 111, 114],
  # '护甲': ['皮', 36, 123,118,125,105],
  #'护甲': ['布', 36, 73, 129, 135, 125],
  #'护甲': ['袍', 30, 71, 109, 119, 128],
  # '护甲': ['荆', 36, 123,118,125,105],
  '头饰': ['巾', 41, 132, 111, 115, 116],
  '技能': [
    #'启程之誓',
    #'启程之心',
    #'启程之风',
    #'破壁之心',
    #'破魔之心',
    #'伤口恶化',
    #'精神创伤',
    #'铁甲尖刺',
    #'沸血之志',
    #'波澜不惊',
    #'飓风之力',
    '' # 结束符,不用管它
  ]
}

def readFileLines(filePath, lineNumbers):
  '''读取文件filePath的所有行,并把lineNumbers行号的文本做成一个列表返回,
    号码超过行号即终止
  '''
  with open(filePath, 'r') as f:
    lines = f.read().split('\n')
    #print(lines)
  result = []
  for i in lineNumbers:
    if i < len(lines):
      result.append(lines[i].strip())
    else:
      break
  #print(result)
  #assert(1<0)
  return result

def makeRole(roleData):
  '''根据roleData,返回一个角色的kfol计算器设定文本'''
  lines = [str(roleData['光环']),'']
  ## 角色资料
  roles = {'琳': 'LIN', '默': 'MO', '艾': 'AI', '梦': 'MENG'}
  role = roles[roleData['角色']]
  lv = roleData['等级']
  skillNumber = roleData['技能位']
  lines.append(f'{role} {lv} {skillNumber}')
  ## 加点
  points = roleData['加点']
  lines.append(f'{points[0]} {points[1]} {points[2]} {points[3]} {points[4]} {points[5]}')
  ## 装备
  equitmentTable = {
    '武器': {'剑': 'SWORD', '杖': 'STAFF', '弓': 'BOW', '刃': 'BLADE'},
    '手套': {'套': 'GLOVES', '环': 'BRACELET'},
    '护甲': {'铁': 'PLATE', '皮': 'LEATHER', '布': 'CLOTH', '袍': 'CLOAK', '荆': 'THORN'},
    '头饰': {'巾': 'SCARF'}
  }
  for equitment in ['武器', '手套', '护甲', '头饰']:
    name = equitmentTable[equitment][roleData[equitment][0]]
    sm = 0
    if len(roleData[equitment]) > 6:
      sm = 1
    points = roleData[equitment][1:6]
    lines.append(f'{name} {points[0]} {points[1]} {points[2]} {points[3]} {points[4]} {sm}')
  ## 技能
  skillTable = {
    '启程之誓': ['SHI', 10], '启程之心': ['XIN', 10], '启程之风': ['FENG', 10],
    '破壁之心': ['BI', 30], '破魔之心': ['MO', 30],
    '伤口恶化': ['SHANG', 50], '精神创伤': ['SHEN', 50], '铁甲尖刺': ['CI', 50],
    '沸血之志': ['FEI', 100], '波澜不惊': ['BO', 100], '飓风之力': ['JU', 100]
  }
  totalPoints = 0
  skills = []
  for skill in roleData['技能']:
    if skill == '':
      continue
    skills.append(skillTable[skill][0])
    totalPoints += skillTable[skill][1]
  if totalPoints > roleData['光环']:
    print('注意!光环加点超标!')
  if len(skills) > roleData['技能位']:
    print('注意!技能数超过技能位数量!')
  skillString = ' '.join(skills)
  lines.append(f'{len(skills)} {skillString}')
  ## 完成,最后组合lines
  return '\n'.join(lines)

def makeFile(filePath, roleData):
  '''根据roleData,重写filePath'''
  roleString = makeRole(roleData)
  otherLines = '\n'.join(readFileLines(filePath, range(10, 10000)))
  text = roleString +'\n\n'+otherLines
  with open(filePath, 'w') as f:
    f.write(text)
    #print(text)
  return



if __name__ == '__main__':
  #print('\n'.join(readFileLines(IN_FILE, range(10,9999))))
  #print(makeRole(role1))
  role = lin1 # 角色选择
  makeFile(IN_FILE, role)
  print('重写文件成功!')